<template>
    <div class="login-box">
        <div class="login-input-box center">
          <h1>信贷管理系统</h1>
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名:" prop="account">
              <el-input type="text" v-model="ruleForm.account" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码:" prop="password">
              <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>
           
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
    </div>
</template>
<script>
import {login} from '@/Api/user'
  export default {
    data() {
      var validateAccount = (rule, value, callback) =>{
        if (value === ''){
            callback(new Error('请输入用户名'))
        }else {
          if (value.length < 3) {
            callback(new Error('用户名的长度不能小于3'))
        }
        callback();
      }
    }
      var validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (value.length < 6) {
            callback(new Error('密码的长度不能小于6'))
        }
          callback();
        }
      };
      
      return {
        ruleForm: {
            account: 'admin',
          password: 'approve123456.',
          
        },
        rules: {
            account: [
            { validator: validateAccount, trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
       submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            // const res = await login(this.ruleForm)
            // console.log(res);
            console.log(this.ruleForm);
            // alert('登陆成功!');
            this.login(this.formName)
          } else {
            console.log('登录失败!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      async login(ruleForm){
        this.$router.replace('/home')
      }
    }
  }
</script>
<style lang="scss" scoped>
.login-input-box {
  width: 650px;
  height: 320px;
  background: #fff;
  text-align: center;
  padding: 40px 40px 12px 12px;
}

.login-box {
    display: flex;
    justify-content: center;
    align-items:center;
    height: 100vh;
    background: url(../assets/bg2.jpg);
    background-size: cover;
}

.el-button {
  width: 100px;
}

.el-input {
  width: 480px;
  margin-bottom: 16px;
}

::v-deep .el-input__inner {
  background: #e5e5e5;
}
</style>
